// 导出excel
export const exportExcel = (res, name, ext) => {
  const blob = new Blob([res.data])
  const fileName = name + '.' + (ext || 'xls')
  const linkNode = document.createElement('a')
  linkNode.download = fileName // a标签的download属性规定下载文件的名称
  linkNode.style.display = 'none'
  linkNode.href = URL.createObjectURL(blob) // 生成一个Blob URL
  document.body.appendChild(linkNode)
  linkNode.click() // 模拟在按钮上的一次鼠标单击
  URL.revokeObjectURL(linkNode.href) // 释放URL 对象
  document.body.removeChild(linkNode)
}

// 递归查询树节点
export const findTreeNodeByDataId = (list, dataId) => {
  let targetNode
  _findNode(list, dataId)
  return targetNode

  function _findNode (list, dataId) {
    for (let i = 0; i < (list.length); i++) {
      if (list[i].dataId === dataId) {
        targetNode = list[i]
        // console.log(targetNode)
      }
      if (list[i].children && list[i].children.length > 0) {
        _findNode(list[i].children, dataId)
      }
    }
  }
}

// 递归查询树节点
export const findTreeNodeById = (list, id) => {
  let targetNode
  _findNode(list, id)
  return targetNode

  function _findNode (list, id) {
    for (let i = 0; i < (list.length); i++) {
      if (list[i].id === id) {
        targetNode = list[i]
        // console.log(targetNode)
      }
      if (list[i].children && list[i].children.length > 0) {
        _findNode(list[i].children, id)
      }
    }
  }
}

// 字典回显单个标签
export const getDictOpt = (options, value) => {
  if ([null, undefined, NaN].includes(value)) {
    return {}
  }
  let item = options.find(x => {
    return value.toString() === x.value.toString()
  })
  if (!item) {
    return {}
  }
  return item
}

// 字典回显多个标签
export const getDictMultipleLabel = (options, values) => {
  if (!values) {
    return '-'
  }
  let valuesArr = []
  if (Array.isArray(values)) {
    valuesArr = values
  } else {
    valuesArr = values.split(',')
  }

  if (valuesArr && valuesArr.length) {
    let labelsArr = []
    valuesArr.forEach(x => {
      let item = options.find(y => {
        return x === y.value
      })
      labelsArr.push(item ? item.label : '-')
    })
    return labelsArr.toString() || '-'
  }
}
